<?php


namespace app\common\service;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Live\Live;
use think\facade\Log;
use think\facade\Env;
use think\Db;
class LiveService
{

    //推流域名
    public $push_domain;
    //推流域名配置的鉴权Key
    public $push_key;
    //生成随机的AppName
    public $appName ;
    //生成随机的StreamName
    public $streamName;
    //配置过期时间为1小时
    public $expireTime;
    //播放域名
    public $play_domain;
    //播放域名配置的鉴权Key
    public $play_key;


    public function __construct()
    {
        $this->push_domain = Env::get('aliyun_live.push_domain');
        $this->push_key = Env::get('aliyun_live.push_key');
        $this->appName = Env::get('aliyun_live.appName');
        $this->streamName = '';
        $this->expireTime = 3600;
        $this->play_domain = Env::get('aliyun_live.play_domain');
        $this->play_key = Env::get('aliyun_live.play_key');
        // 阿里云账号AccessKey拥有所有API的访问权限，建议您使用RAM用户进行API访问或日常运维。
        // 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里，否则可能导致AccessKey泄露，威胁您账号下所有资源的安全。
        // 本示例通过从配置文件中读取AccessKey，来实现API访问的身份验证。
        AlibabaCloud::accessKeyClient(Env::get('aliyun_live.access_key_id'), Env::get('aliyun_live.access_key_secret'))->asDefaultClient();
    }

    /**
     * 生成推流地址
     * @param $push_domain 推流域名
     * @param $push_key 推流域名配置的鉴权Key
     * @param $expireTime 过期时间（单位是秒）
     * @param $appName 推流AppName
     * @param $streamName 推流StreamName
     */
    //public function push_url($push_domain,$push_key,$expireTime,$appName,$streamName){
    public function push_url($push_domain,$push_key,$expireTime,$appName,$streamName){
        $push_url = '';
        //未开启鉴权Key的情况下
        if($push_key==''){
            $push_url = 'rtmp://'.$push_domain.'/'.$appName.'/'.$streamName;
            return $push_url;
        }
        $timeStamp = time() + $expireTime;
        $sstring = '/'.$appName.'/'.$streamName.'-'.$timeStamp.'-0-0-'.$push_key;
        $md5hash = md5($sstring);
        $push_url = 'rtmp://'.$push_domain.'/'.$appName.'/'.$streamName.'?auth_key='.$timeStamp.'-0-0-'.$md5hash;
        return $push_url;
    }
    /**
     * 生成播放地址
     * @param $play_domain 播放域名
     * @param $play_key 播放鉴权Key
     * @param $expireTime 过期时间（单位是秒）
     * @param $appName 播放appName（同推流appName)
     * @param $streamName 播放streamName，播放源流时，streamName 同推流streamName；播放转码流时，streamName 为推流streamName_{转码模板ID}
     */
    public function play_url($play_domain,$play_key,$expireTime,$appName,$streamName){
        //未开启鉴权Key的情况下
        if($play_key==''){
            $rtmp_play_url = 'rtmp://'.$play_domain.'/'.$appName.'/'.$streamName;
            $flv_play_url = 'https://'.$play_domain.'/'.$appName.'/'.$streamName.'.flv';
            $hls_play_url = 'https://'.$play_domain.'/'.$appName.'/'.$streamName.'.m3u8';
        }else{
            $timeStamp = time() + $expireTime;

            $rtmp_sstring = '/'.$appName.'/'.$streamName.'-'.$timeStamp.'-0-0-'.$play_key;
            $rtmp_md5hash = md5($rtmp_sstring);
            $rtmp_play_url = 'rtmp://'.$play_domain.'/'.$appName.'/'.$streamName.'?auth_key='.$timeStamp.'-0-0-'.$rtmp_md5hash;

            $flv_sstring = '/'.$appName.'/'.$streamName.'.flv-'.$timeStamp.'-0-0-'.$play_key;
            $flv_md5hash = md5($flv_sstring);
            $flv_play_url = 'https://'.$play_domain.'/'.$appName.'/'.$streamName.'.flv?auth_key='.$timeStamp.'-0-0-'.$flv_md5hash;

            $hls_sstring = '/'.$appName.'/'.$streamName.'.m3u8-'.$timeStamp.'-0-0-'.$play_key;
            $hls_md5hash = md5($hls_sstring);
            $hls_play_url = 'https://'.$play_domain.'/'.$appName.'/'.$streamName.'.m3u8?auth_key='.$timeStamp.'-0-0-'.$hls_md5hash;
        }

        $arr = [
            'rtmp'=>$rtmp_play_url,
            'flv'=>$flv_play_url,
            'hls'=>$hls_play_url,
        ];
        return $arr;
    }

    public function get_push_url($time,$stream)
    {
        if(!isset($time) ||empty($time))
        {
            $time = $this->expireTime;
        }
        if(!isset($stream) ||empty($stream))
        {
            $stream =  date('YmdHis');
        }
        return $this->push_url($this->push_domain,$this->push_key,$time,$this->appName,$stream);
    }

    public function get_play_url($time,$stream)
    {
        if(!isset($time) ||empty($time))
        {
            $time = $this->expireTime;
        }
        if(!isset($stream) ||empty($stream))
        {
            $stream =  date('YmdHis');
        }
        return $this->play_url($this->play_domain,$this->play_key,$time,$this->appName,$stream);
    }
}